#include "Landscape.h"

CLandscape::CLandscape(void)
{

}

//////////////////////////////////////////////////////////////////////////

CLandscape::CLandscape( LPDIRECT3DDEVICE9 pDevice, HWND pHandle ) : 
CRender(pDevice, pHandle)
{
	
}

//////////////////////////////////////////////////////////////////////////

CLandscape::~CLandscape(void)
{
	
}

//////////////////////////////////////////////////////////////////////////

void CLandscape::Create()
{
	
}

//////////////////////////////////////////////////////////////////////////

void CLandscape::Reset()
{

}

//////////////////////////////////////////////////////////////////////////

void CLandscape::Lost()
{

}

//////////////////////////////////////////////////////////////////////////

void CLandscape::Move(float time)
{	
	
}

//////////////////////////////////////////////////////////////////////////

void CLandscape::Render()
{

}

//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////
CTestLandscape::CTestLandscape( void )
{
	
}

CTestLandscape::CTestLandscape( LPDIRECT3DDEVICE9 pDevice, HWND pHandle ) :
CRender(pDevice, pHandle),

mGridX(128),
mGridY(128),
mGridSize(1.0f),

mVertexBuffer(NULL),
mIndexBuffer(NULL),

mVertexArray(NULL),
mIndexArray(NULL),
mTextureBuffer(NULL)
{
	
}

CTestLandscape::~CTestLandscape( void )
{
	SAFE_RELEASE(mTextureBuffer);
}

void CTestLandscape::Create()
{
	WCHAR tTextureFileName[255];

	wsprintf(tTextureFileName, L"%s", L"Content/ground.dds");
	D3DXCreateTextureFromFile(mDevice, tTextureFileName, &mTextureBuffer);

	assert(mTextureBuffer != NULL);

}

void CTestLandscape::Reset()
{
	
}

void CTestLandscape::Lost()
{

}

void CTestLandscape::Move( float time )
{

}

void CTestLandscape::Render()
{

}

void CTestLandscape::FillArray()
{
	mVertexCount = mGridX * mGridY;
	mVertexArray = new STerrain[mVertexCount];

	for(DWORD y = 0; y < mGridY; y++){
		for(DWORD x = 0; x < mGridX; x++){
			mVertexArray[x + y * (mGridX)].mPosition.x = (float)x * mGridSize;
			mVertexArray[x + y * (mGridX)].mPosition.y = 0.0f;
			mVertexArray[x + y * (mGridX)].mPosition.z = (float)y * mGridSize;

			mVertexArray[x + y * (mGridX)].mNormal.x = 0.0f;
			mVertexArray[x + y * (mGridX)].mNormal.y = 1.0f;
			mVertexArray[x + y * (mGridX)].mNormal.z = 0.0f;

			mVertexArray[x + y * (mGridX)].mDiffuse = 0x00DDDDDD;
			mVertexArray[x + y * (mGridX)].mCoord= D3DXVECTOR2((float)(x), (float)(y));
		}
	}

	assert(mVertexArray[129].mPosition.x == 1.0f);
	assert(mVertexArray[129].mPosition.z == 1.0f);
}

void CTestLandscape::CreateVertexBuffer()
{
	mDevice->CreateVertexBuffer(mVertexCount * sizeof(STerrain), 0, FVF_TERRAIN_GRID, D3DPOOL_DEFAULT, &mVertexBuffer, NULL);

	STerrain* tVertex = NULL;
	mVertexBuffer->Lock(0, mVertexCount * sizeof(STerrain), (void **)&tVertex, 0);
	for(DWORD i = 0; i < mVertexCount; i++) tVertex[i] = mVertexArray[i];
	mVertexBuffer->Unlock();

	assert(mVertexBuffer != NULL);
}